package b2.widget.tank
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.ObjectItem;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;
	
	public class EquipSlot extends Sprite
	{
//		public var isEquiped:Boolean;
		private var position:int;
		private var comIndex:String;
		private var equipItem:ObjectItem;
		private var unavailableBgr:Sprite;
		private var posBgr:Sprite;
		private var tip:BasicMessage;
		
		public function EquipSlot(position:int)
		{
			super();
			this.position = position;
			//加载空槽位
			var rrect:Sprite = new Sprite();
			rrect.graphics.lineStyle(2.5, 0x27AAE1);
			rrect.graphics.beginFill(0x003752);
			rrect.graphics.drawRoundRect(0, 0, 50, 50, 26, 26);
			rrect.graphics.endFill();
			rrect.filters = [new DropShadowFilter(2, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			addChild(rrect);
			//位置标志
			var PosBgr:Class = ResourceHelper.getInstance().getClassByName("Equip" + position + "_0");
			posBgr = new PosBgr();
			posBgr.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			posBgr.alpha = 0.2;
			posBgr.width = 38;
			posBgr.height = 38;
			posBgr.x = 8;
			posBgr.y = 6;
			addChild(posBgr);
			//禁用标志
			var EquipSlotBgr2:Class = ResourceHelper.getInstance().getClassByName("EquipSlotBgr2");
			unavailableBgr = new EquipSlotBgr2();
			unavailableBgr.visible = false;
			unavailableBgr.alpha = 0.2;
			addChild(unavailableBgr);
//			this.isEquiped = false;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function get isEquiped():Boolean
		{
			return (equipItem != null && equipItem.parent != null)
		}
		
		public function loadEquip(comIndex:String, location:int = 2):void
		{
			removeEquip();
			this.comIndex = comIndex;
			var objIndexArr:Array = comIndex.split("_");
			equipItem = new ObjectItem(objIndexArr[0], objIndexArr[1], objIndexArr[2], objIndexArr[3], 0, location, false);
			addChild(equipItem);
		}
		
		public function removeEquip():void
		{
			if (equipItem != null && equipItem.parent != null)
				equipItem.parent.removeChild(equipItem);
			equipItem = null;
		}
		
		public function refresh():void
		{
			var tankId:int = TankDialog.getInstance().tankSlot.tankId;
			var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			unavailableBgr.visible = TankConfig.EQUIP_SLOT_AVAILABLE[position] > tankInfo.basicLevel;
			posBgr.visible = !unavailableBgr.visible;
		}

		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			if (!isEquiped && tip == null)
			{
				var tankId:int = TankDialog.getInstance().tankSlot.tankId;
				var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
				var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
				var tipMessage:String;
				if (TankConfig.EQUIP_SLOT_AVAILABLE[position] > tankInfo.basicLevel)
				{
					var requireQuality:String = Config.basicConfig.tankBasicLevel[TankConfig.EQUIP_SLOT_AVAILABLE[position]];
					tip = new BasicMessage(Config.basicConfig.equipPosName[position] + ", 坦克品质达到" + requireQuality + "时开启");
				}
				else
				{
					tip = new BasicMessage(Config.basicConfig.equipPosName[position]);
				}
				tip.x = Math.max(0, globalPoint.x - tip.width / 2 + 25);
				tip.y = globalPoint.y + 50;
				SceneManager.getInstance().currentScene.addChild(tip);
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (tip != null && tip.parent != null)
			{
				tip.parent.removeChild(tip);
				tip = null;
			}
		}
		
	}
}